/*!
 * \file MechManiaClient.h
 */
#pragma once

#include "TcpSocket.h"
#include <cstdlib>
#include <sstream>
#include <iomanip>

namespace Networking
{
	/*!
	 * \class MechManiaClient
	 * \brief Encapsulates TcpSocket to do the double send and such.
	 */
	class MechManiaClient
	{
public:

		/*!
		 * \brief Constructor
		 *
		 * A constructor that creates a new socket
		 */
		MechManiaClient ();

		/*!
		 * \brief Destructor
		 *
		 * A destructor that shutsdown the sock and deletes the socket
		 */
		~MechManiaClient ();

		/*!
		 * \brief Connects to an ip address on a port
		 *
		 * \param ip The ip address of the server running.
		 * \param port The port of the running server
		 */
		void Connect( const std::string & ip, unsigned short port );

		/*!
		 * \brief Listens on a certain port
		 *
		 * \param port The port to listen on.
		 */

		/*!
		 * \brief Accept a connection
		 *
		 * \return A mechmania client with an open socket.
		 */

		/*!
		 * \brief Sends a turn including the double send.
		 *
		 * \param xmlMessage The entire xml message to send in the packet.
		 */
		void SendTurn( const std::string & xmlMessage );

		/*!
		 * \brief Receives a turn from the server
		 *
		 * \return A string holding the contents of the received packet.
		 */
		std::string ReceiveTurn ();

		/*!
		 * \brief Gets the TcpSocket object that this Client uses.
		 *
		 * \return A TcpSocket* holding the TcpSocket that this Client uses.
		 */

	private:

		char *               m_receivingBuffer;/*< The current message of the
		                                           client.*/
		long                 m_receivingBufferLength;/*< The length of the
		                                                 received message.*/

		TcpSocket * GetUnderlyingTcpSocket() { return this->m_socket; }
		TcpSocket *             m_socket;/*< Stores the underlying TcpSocket.*/
		static const int        MessageSizeLength = 10;/*< Stores the message size
		                                                   length.*/
		void Listen( unsigned short port );
		MechManiaClient * Accept ();
		/*!
		 * \brief Constructor
		 *
		 * A constructor that uses a TcpSocket passed into it.
		 */
		MechManiaClient(TcpSocket * socket);

		char * AllocateMessageBuffer( long size );
	};
}
